package com.sdjenkins.test;

import java.io.FileWriter;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.sdjenkins.sms.Collector;
import com.sdjenkins.sms.Sms;
import com.sdjenkins.sms.Smses;
import com.sdjenkins.sms.WASms;
import com.sdjenkins.sms.srv.Service;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;

public class Load {
	
	Session session = Service.getSession();	

	public static void main(String[] args) {
		Load load = new Load();
		load.go();
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Smses load() {
		
		  Smses smses = new Smses();
		  
		try {
			  String SQL_QUERY ="from Collector";
			  Query query = session.createQuery(SQL_QUERY);
			  List x = query.list();
			  smses.setSms(x);
			}
			catch(Exception e){
			  System.out.println(e.getMessage());
			}
		return smses;
				
	}
	

	private void go() {
		

		Transaction tr = session.beginTransaction();
		write(load());
		
		tr.commit();
		session.close();
		
	}
	
	private void write(Smses smses) {
		
 	    XStream xstream = new XStream(new StaxDriver());
		xstream.alias("sms", Sms.class);
		xstream.alias("smses", Smses.class);
		
		xstream.useAttributeFor(Smses.class, "count");
	
        xstream.useAttributeFor(Sms.class, "address"); 
        xstream.useAttributeFor(Sms.class, "type");
        xstream.useAttributeFor(Sms.class, "body");
        xstream.useAttributeFor(Sms.class, "protocol");
        xstream.useAttributeFor(Sms.class, "read");
        xstream.useAttributeFor(Sms.class, "status");
        xstream.useAttributeFor(Sms.class, "toa");
        xstream.useAttributeFor(Sms.class, "sc_toa");
        xstream.useAttributeFor(Sms.class, "service_center");        
        xstream.useAttributeFor(Sms.class, "subject");
        xstream.useAttributeFor(Sms.class, "date");        
        
        xstream.addImplicitCollection(Smses.class, "sms", WASms.class);
		try {
			FileWriter fw = new FileWriter("wa.xml");
			xstream.toXML(smses, fw);
			fw.close();
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
        
		
		
		
		
		
		
		
		
		
		
		
		
	}

	
}
